﻿using System;
using System.IO;

namespace CXW.Core.Services
{
    /// <summary>
    /// IMessageService implementation that writes messages to a text writer.
    /// User input is not implemented by this service.
    /// </summary>
    public class TextWriterMessageService : IMessageService
    {
        readonly TextWriter writer;

        public TextWriterMessageService(TextWriter writer)
        {
            if (writer == null)
                throw new ArgumentNullException("writer");
            this.writer = writer;
        }

        public bool ShowError(string message, string caption = null)
        {
            writer.WriteLine(message); return default(bool);
        }

        public bool ShowException(Exception ex, string message)
        {
            if (message != null)
            {
                writer.WriteLine(message);
            }
            if (ex != null)
            {
                writer.WriteLine(ex.ToString());
            }
            return default(bool);
        }

        public void ShowWarning(string message, string caption = null)
        {
            writer.WriteLine(message);
        }

        public bool AskQuestion(string question, string caption = null)
        {
            writer.WriteLine(caption + ": " + question);
            return false;
        }

        public void ShowMessage(string message, string caption = null)
        {
            writer.WriteLine(caption + ": " + message);
        }
    }
}
